package io.renren.modules.archive.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.StringUtils;
import io.renren.modules.archive.entity.ArchiveCollectEntity;

import io.renren.modules.archive.entity.BgqxEntity;
import io.renren.modules.archive.entity.DictionaryEntity;
import io.renren.modules.archive.entity.MeetingtypeEntity;
import io.renren.modules.archive.service.*;
import io.renren.modules.sys.entity.SysMenuEntity;
import io.renren.modules.sys.service.SysMenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * ${comments}
 *
 * @author lizeliang
 * @email 2749877017@qq.com
 * @date 2023-08-21 17:07:01
 */
@RestController
@RequestMapping("archive/dictionary")
public class DictionaryController {
    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private BgqxService bgqxService;
    @Autowired
    private ArchiveCollectService archiveCollectService;
    @Autowired
    private MeetingtypeService meetingtypeService;
    @Autowired
    private NdJgMenuService ndJgMenuService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("archive:dictionary:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = dictionaryService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 不分页列表
     */
    @RequestMapping("/query")
    public R query(@RequestParam Map<String, Object> params){
        List<DictionaryEntity> dictList= dictionaryService.queryAll(params);

        return R.ok().put("list", dictList)

                ;
    }


    /**
     * 字典列表
     */
    @RequestMapping("/getDictDataList")
    public R getDictDataList(@RequestParam Map<String, Object> params){
        List<DictionaryEntity> geocodeList=dictionaryService.getDictDataListByType("geocode");
        List<DictionaryEntity> jgList=dictionaryService.getDictDataListByType("nsjgmc");
        List<DictionaryEntity> dwmcList=dictionaryService.getDictDataListByType("dwmc");
        List<DictionaryEntity> bgqxList=dictionaryService.getDictDataListByType("bgqx");
        List<ArchiveCollectEntity>    menuList=archiveCollectService.queryLocalMenu(params);
        String id=(String)params.get("menuId");
        String level=(String)params.get("level");
        Long ID=Long.valueOf(id);
        String onesxlx="";
        String twosxlx="";
        String threesxlx="";
        String foursxlx="";
        if(menuList.size()>0){
            if(menuList.size()==1){
                onesxlx= menuList.get(0).getValue();
            }else if(menuList.size()==2){
                onesxlx= menuList.get(menuList.size()-1).getValue();
                twosxlx= menuList.get(menuList.size()-2).getValue();
            }else if(menuList.size()==3){
                onesxlx= menuList.get(menuList.size()-1).getValue();
                twosxlx= menuList.get(menuList.size()-2).getValue();
                threesxlx= menuList.get(0).getValue();
            }else{
                onesxlx= menuList.get(menuList.size()-1).getValue();
                twosxlx= menuList.get(menuList.size()-2).getValue();
                threesxlx= menuList.get(menuList.size()-3).getValue();
                foursxlx= menuList.get(0).getValue();
            }
        }
        MeetingtypeEntity meetingtypeEntity=new MeetingtypeEntity();
        meetingtypeEntity.setOnesxlx(onesxlx);
        meetingtypeEntity.setTwosxlx(twosxlx);
        meetingtypeEntity.setThreesxlx(threesxlx);
        meetingtypeEntity.setFoursxlx(foursxlx);
        meetingtypeEntity.setMeetingtypeJg((String)params.get("jg"));
        meetingtypeEntity.setMeetingtypeNd((String)params.get("nd"));
        List<Long> ndJgMenuList=ndJgMenuService.getNdJgMenuList(params);
        List<ArchiveCollectEntity>  foursxlxList=new ArrayList<>();
        if(level.equals("3")){
            foursxlxList= archiveCollectService.queryMenuByLevelAndParentId(ndJgMenuList,ID);
        }else if(level.equals("4")){
            foursxlxList= archiveCollectService.queryMenuByLevelAndMenuId(ndJgMenuList,ID);
        }


        QueryWrapper<BgqxEntity> gdcl=new QueryWrapper<>();
        gdcl.eq(StringUtils.isNotBlank(onesxlx),"ONESXLX",onesxlx)
                .eq(StringUtils.isNotBlank(twosxlx),"TWOSXLX",twosxlx)
                .eq(StringUtils.isNotBlank(threesxlx),"THREESXLX",threesxlx);

        List<BgqxEntity> gdclList=bgqxService.list(gdcl);

        return R.ok().put("geocodeList", geocodeList)
                .put("bgqxList", bgqxList)
                .put("gdclList", gdclList)
                .put("dwmcList",dwmcList)
                .put("jgList",jgList)
                .put("foursxlxList",foursxlxList)
                .put("onesxlx", onesxlx)
                .put("twosxlx",twosxlx)
                .put("threesxlx",threesxlx)
                .put("foursxlx",foursxlx);
    }


    /**
     * 字典列表
     */
    @RequestMapping("/getDictList")
    public R getDictList(){
        List<DictionaryEntity> geocodeList=dictionaryService.getDictDataListByType("geocode");
        List<DictionaryEntity> jgList=dictionaryService.getDictDataListByType("nsjgmc");
        List<DictionaryEntity> bgqxList=dictionaryService.getDictDataListByType("bgqx");
        ArchiveCollectEntity archiveCollectEntity=new ArchiveCollectEntity();
        archiveCollectEntity.setLevel("1");
        List<ArchiveCollectEntity> oneList=archiveCollectService.selectByParentIdAndLevel(archiveCollectEntity);
        return R.ok().put("geocodeList", geocodeList)
                .put("bgqxList", bgqxList)
                .put("jgList",jgList)
                .put("oneList",oneList)
                ;
    }

    /**
     * 机构字典列表
     */
    @RequestMapping("/getJgDictDataList")
    public R getJgDictDataList() {
        List<DictionaryEntity> jgList=dictionaryService.getDictDataListByType("nsjgmc");
        return R.ok().put("jgList",jgList);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{dictionaryId}")
//    @RequiresPermissions("archive:dictionary:info")
    public R info(@PathVariable("dictionaryId") Long dictionaryId){
		DictionaryEntity dictionary = dictionaryService.getById(dictionaryId);

        return R.ok().put("dictionary", dictionary);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("archive:dictionary:save")
    public R save(@RequestBody DictionaryEntity dictionary){
		dictionaryService.save(dictionary);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("archive:dictionary:update")
    public R update(@RequestBody DictionaryEntity dictionary){
		dictionaryService.updateById(dictionary);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("archive:dictionary:delete")
    public R delete(@RequestBody Long[] dictionaryIds){
		dictionaryService.removeByIds(Arrays.asList(dictionaryIds));

        return R.ok();
    }



    @RequestMapping("/selectTwoList")
    public R selectTwoList(@RequestParam  Map<String, Object> params){
        String parentId=(String)params.get("parentId");
        Long parentID=Long.valueOf(parentId);
        ArchiveCollectEntity archiveCollectEntity=new ArchiveCollectEntity();
        archiveCollectEntity.setParentId(parentID);
        archiveCollectEntity.setLevel("2");
        List<ArchiveCollectEntity> twoList= archiveCollectService.selectByParentIdAndLevel(archiveCollectEntity);
        return R.ok()
                .put("twoList",twoList)
                ;
    }




    @RequestMapping("/selectThreeList")
    public R selectThreeList(@RequestParam Map<String, Object> params){
        String parentId=(String)params.get("parentId");
        Long parentID=Long.valueOf(parentId);
        ArchiveCollectEntity archiveCollectEntity=new ArchiveCollectEntity();
        archiveCollectEntity.setParentId(parentID);
        archiveCollectEntity.setLevel("3");
        List<ArchiveCollectEntity> threeList= archiveCollectService.selectByParentIdAndLevel(archiveCollectEntity);

       return R.ok()
                .put("threeList",threeList)
                ;
    }

    @RequestMapping("/selectFourList")
    public R selectFourList(@RequestParam Map<String, Object> params
    ){
        String parentId=(String)params.get("parentId");
        Long parentID=Long.valueOf(parentId);
        ArchiveCollectEntity archiveCollectEntity=new ArchiveCollectEntity();
        archiveCollectEntity.setParentId(parentID);
        archiveCollectEntity.setLevel("4");
        List<ArchiveCollectEntity> fourList= archiveCollectService.selectByParentIdAndLevel(archiveCollectEntity);

        return R.ok()
                .put("fourList",fourList)
                ;
    }


}
